import { type RouteRecordRaw, createRouter } from "vue-router"
import { history, flatMultiLevelRoutes } from "./helper"
import routeSettings from "@/config/route"

const Layouts = () => import("@/layouts/index.vue")

/**
 * 常驻路由
 * 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置 Name 属性
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layouts,
    meta: {
      hidden: true
    },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue")
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/views/error-page/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/views/error-page/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/",
    // component: Layouts,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          svgIcon: "dashboard",
          affix: true
        }
      }
    ]
  },
]

/**
 * 动态路由
 * 用来放置有权限 (Roles 属性) 的路由
 * 必须带有 Name 属性
 */
export const asyncRoutes: RouteRecordRaw[] = [
  {
    path: "/:pathMatch(.*)*", // Must put the 'ErrorPage' route at the end, 必须将 'ErrorPage' 路由放在最后
    redirect: "/404",
    name: "ErrorPage",
    meta: {
      hidden: true
    }
  },
  {
    path: "/shuttlebus",
    component: Layouts,
    redirect: "/shuttlebus/index",
    meta: {
      title: "物流配送",
      roles: ["admin", 'wl'],
      svgIcon: "ck"
    },
    children: [
      {
        path: "query",
        component: () => import("@/views/car/query.vue"),
        name: "query",
        meta: {
          title: "订单类型",
          svgIcon: "ck"
        }
      },
      {
        path: "special",
        component: () => import("@/views/label-manage/special-vehicle.vue"),
        name: "speciali",
        meta: {
          title: "专车配送",
          svgIcon: "ck"
        }
      },
      {
        path: "index",
        component: () => import("@/views/shuttlebus/index.vue"),
        name: "shuttlebusi",
        meta: {
          title: "物流班车",
          svgIcon: "ck"
        }
      },
      {
        path: "urgent",
        component: () => import("@/views/label-manage/special-urgent.vue"),
        name: "urgent",
        meta: {
          title: "物流急件",
          svgIcon: "ck"
        }
      },
    ]
  },
  {
    path: "/car",
    component: Layouts,
    redirect: "/car/heatmap",
    meta: {
      // title: "设备管理",
      roles: ["admin", 'wl'],
      svgIcon: "car"
    },
    children: [
      // {
      //   path: "specialCar",
      //   component: () => import("@/views/car/specialCar.vue"),
      //   name: "specialCar",
      //   meta: {
      //     title: "专车",
      //   }
      // },
      // {
      //   path: "shuttleBus",
      //   component: () => import("@/views/car/shuttleBus.vue"),
      //   name: "shuttleBus",
      //   meta: {
      //     title: "班车",
      //   }
      // },
      {
        path: "heatmap",
        component: () => import("@/views/car/heatmap.vue"),
        name: "heatmap",
        meta: {
          title: "设备管理",
          svgIcon: "car"
        }
      },
    ]
  },
  {
    path: "/plan",
    component: Layouts,
    redirect: "/plan/index",
    meta: {
      roles: ["admin", "wl",],
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/plan/index.vue"),
        name: "plani",
        meta: {
          title: "物流计划",
          svgIcon: "ck"
        }
      },
      {
        path: "inventory",
        component: () => import("@/views/plan/inventory.vue"),
        name: "inventory",
        meta: {
          title: "配送清单",
          hidden: true
        }
      },
      {
        path: "workstation",
        component: () => import("@/views/plan/workstation.vue"),
        name: "workstation",
        meta: {
          title: "工位",
          hidden: true
        }
      },
    ]
  },
  {
    path: "/security",
    component: Layouts,
    redirect: "/security/index",
    meta: {
      roles: ["admin", 'wl'],
      svgIcon: "ck"
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/problem/security.vue"),
        name: "security",
        meta: {
          title: "物流安全",
          svgIcon: "ck"
        }
      }
    ]
  },
  {
    path: "/comprehensive",
    component: Layouts,
    redirect: "/comprehensive/index",
    meta: {
      roles: ["admin"],
      svgIcon: "ck"
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/problem/comprehensive.vue"),
        name: "comprehensive",
        meta: {
          title: "综合事务",
          svgIcon: "ck"
        }
      }
    ]
  },
  {
    path: "/taskOverview",
    component: Layouts,
    redirect: "/taskOverview/list",
    meta: {
      title: "任务看板",
      roles: ["admin", "ck",],
      svgIcon: "ck"
    },
    children:[
      {
        path: "list",
        component: () => import("@/views/car/taskOverview.vue"),
        name: "taskOverview",
        meta: {
          title: "任务看板",
          svgIcon: "dashboard"
        }
      }
    ]
  },
  {
    path: "/warehouse",
    component: Layouts,
    redirect: "/warehouse/list",
    meta: {
      title: "仓储管理",
      roles: ["admin", "ck",],
      svgIcon: "ck"
    },
    children: [
      {
        path: "list",
        component: () => import("@/views/car/warehouse.vue"),
        name: "warehouse",
        meta: {
          title: "仓库管理",
        }
      },
      {
        path: "JobID",
        component: () => import("@/views/JobID/index.vue"),
        name: "JobID",
        meta: {
          title: "产品工号管理",
        }
      },
      {
        path: "list1",
        component: () => import("@/views/car/warehouseDetail.vue"),
        name: "warehouse1",
        meta: {
          title: "1#库",
        }
      },
      {
        path: "list2",
        component: () => import("@/views/car/warehouseDetail.vue"),
        name: "warehouse2",
        meta: {
          title: "2#库",
        }
      },
      {
        path: "ckimg",
        component: () => import("@/views/car/ckimg.vue"),
        name: "ckimg",
        meta: {
          title: "仓库示意图",
        }
      },
      {
        path: "inventorying",
        component: () => import("@/views/label-manage/inventorying.vue"),
        name: "inventorying",
        meta: {
          title: "盘库",
          roles: ["admin", "ck"],
        }
      },
      {
        path: "inventory",
        component: () => import("@/views/label-manage/inventory.vue"),
        name: "Inventory",
        meta: {
          title: "已盘库记录",
          roles: ["admin", "ck"],
        }
      }
    ]
  },
  {
    path: "/supplier",
    component: Layouts,
    meta: {
      title: "物料查询",
      roles: ["admin", 'yh'],
      svgIcon: "search"
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/supplier/index.vue"),
        name: "supplier",
        meta: {
          title: "物料查询",
        }
      },
      {
        path: "car",
        component: () => import("@/views/supplier/car.vue"),
        name: "suppliercar",
        meta: {
          title: "购物车",
        }
      },
      {
        path: "tray",
        component: () => import("@/views/supplier/tray.vue"),
        name: "tray",
        meta: {
          title: "托盘清单",
        }
      },
    ]
  },
  {
    path: "/labels",
    component: Layouts,
    meta: {
      title: "订单管理",
      svgIcon: "ddd",
      hidden: true
    },
    children: [
      {
        path: "spotcheck",
        component: () => import("@/views/problem/spotcheck.vue"),
        name: "spotcheck",
        meta: {
          title: "点检表",
        }
      },
      {
        path: "secDetail",
        component: () => import("@/views/problem/secDetail.vue"),
        name: "secDetail",
        meta: {
          title: "安全详情",
        }
      },
      {
        path: "study",
        component: () => import("@/views/problem/study.vue"),
        name: "study",
        meta: {
          title: "在线学习",
        }
      },
      {
        path: "studyDeta",
        component: () => import("@/views/problem/studyDeta.vue"),
        name: "studyDeta",
        meta: {
          title: "学习详情",
        }
      },
      {
        path: "year",
        component: () => import("@/views/problem/year.vue"),
        name: "year",
        meta: {
          title: "违章统计",
        }
      },
      {
        path: "handover",
        component: () => import("@/views/problem/handover.vue"),
        name: "Handover",
        meta: {
          title: "交接班记录",
        }
      },
      {
        path: "maintain",
        component: () => import("@/views/problem/maintain.vue"),
        name: "maintain",
        meta: {
          title: "维修保养记录",
        }
      },
      {
        path: "journal",
        component: () => import("@/views/label-manage/journal.vue"),
        name: "journal",
        meta: {
          title: "工作日志",
          hidden: true
        }
      },
      {
        path: "warehouseDetail",
        component: () => import("@/views/car/warehouseDetail.vue"),
        name: "warehouseDetail",
        meta: {
          title: "仓库详情",
          hidden: true
        }
      },
      {
        path: "jimg",
        component: () => import("@/views/plan/img.vue"),
        name: "jimg",
        meta: {
          title: "工位示意图",
          hidden: true
        }
      },
      {
        path: "work",
        component: () => import("@/views/car/work.vue"),
        name: "work",
        meta: {
          title: "车辆工作记录",
          hidden: true
        }
      },
      {
        path: "problemAdd",
        component: () => import("@/views/problem/add.vue"),
        name: "problemAdd",
        meta: {
          title: "点检表查看",
          hidden: true
        }
      },
      {
        path: "shippingDetail",
        component: () => import("@/views/shipping/shippingDetail.vue"),
        name: "shippingDetail",
        meta: {
          title: "发货单详情",
          hidden: true
        }
      },
      {
        path: "shippingrk",
        component: () => import("@/views/shipping/rk.vue"),
        name: "shippingrk",
        meta: {
          title: "发货单入库",
          hidden: true
        }
      },
      {
        path: "distribution",
        component: () => import("@/views/shuttlebus/distribution.vue"),
        name: "distribution",
        meta: {
          title: "人员分配",
          hidden: true
        }
      },
      // {
      //   path: "shuttlebusadd",
      //   component: () => import("@/views/shuttlebus/add.vue"),
      //   name: "shuttlebusadd",
      //   meta: {
      //     title: "新建转运单-班车",
      //     hidden: true
      //   }
      // },
      {
        path: "site",
        component: () => import("@/views/shuttlebus/site.vue"),
        name: "site",
        meta: {
          title: "站点详情",
          hidden: true
        }
      },
      {
        path: "detail-sos",
        component: () => import("@/views/label-manage/detail-sos.vue"),
        name: "lldetail-sos",
        meta: {
          title: "领料单详情",
          // svgIcon: "dd",
          hidden: true
        }
      },
      {
        path: "cangku",
        component: () => import("@/views/label-manage/cangku.vue"),
        name: "cangku",
        meta: {
          title: "出库单详情",
          svgIcon: "unocss",
          hidden: true
        }
      },
      {
        path: "peisong",
        component: () => import("@/views/label-manage/peisong.vue"),
        name: "peisong",
        meta: {
          title: "配送单详情",
          svgIcon: "unocss",
          hidden: true
        }
      },
      {
        path: "urgentpeisong",
        component: () => import("@/views/label-manage/peisong-urgent.vue"),
        name: "urgentpeisong",
        meta: {
          title: "急件配送单详情",
          svgIcon: "unocss",
          hidden: true
        }
      },
      {
        path: "detail",
        component: () => import("@/views/label-manage/detail.vue"),
        name: "zydxq",
        meta: {
          title: "转运单详情",
          svgIcon: "unocss",
          hidden: true
        }
      },
      {
        path: "rkdxq",
        component: () => import("@/views/label-manage/rkdxq.vue"),
        name: "rkdxq",
        meta: {
          title: "入库单详情",
          svgIcon: "unocss",
          hidden: true
        }
      },
      {
        path: "tkdxq",
        component: () => import("@/views/label-manage/tkdxq.vue"),
        name: "tkdxq",
        meta: {
          title: "退库单详情",
          svgIcon: "unocss",
          hidden: true
        }
      },
      {
        path: "overxq",
        component: () => import("@/views/label-manage/overxq.vue"),
        name: "overxq",
        meta: {
          title: "已完成详情",
          svgIcon: "unocss",
          hidden: true
        }
      },
      {
        path: "add",
        component: () => import("@/views/label-manage/add.vue"),
        name: "lladd",
        meta: {
          title: "新建领料单",
          hidden: true,
        }
      },
      {
        path: "addzyd",
        component: () => import("@/views/label-manage/addzyd.vue"),
        name: "addzyd",
        meta: {
          title: "新建转运单",
          hidden: true,
        }
      },
      {
        path: "addrkd",
        component: () => import("@/views/label-manage/addrkd.vue"),
        name: "addrkd",
        meta: {
          title: "新建入库单",
          hidden: true,
        }
      },
      {
        path: "card-manage/detail",
        component: () => import("@/views/card-manage/approvalDetail.vue"),
        name: "approvalDetail",
        meta: {
          title: "审批详情",
          hidden: true
        }
      },
      {
        path: "card-manage/pressingDetail",
        component: () => import("@/views/card-manage/pressingApprovalDetail.vue"),
        name: "pressingApprovalDetail",
        meta: {
          title: "加急审批详情",
          hidden: true
        }
      },
      {
        path: "role/save",
        component: () => import("@/views/system/role/save.vue"),
        name: "systemRoleSave",
        meta: {
          title: "角色编辑",
          hidden: true
        }
      },
      {
        path: "inventoryDetail",
        component: () => import("@/views/label-manage/inventoryDetail.vue"),
        name: "inventoryDetail",
        meta: {
          title: "库位详情",
          roles: ["admin", "ck"],
          hidden: true
        }
      },
      {
        path: "taskDetail",
        component: () => import("@/views/car/taskDetail.vue"),
        name: "taskDetail",
        meta: {
          title: "分配详情",
          svgIcon: "dashboard"
        }
      }
    ]
  },
  {
    path: "/label-manage",
    component: Layouts,
    meta: {
      title: "订单管理",
      svgIcon: "dd",
      roles: ['admin', 'ck', 'yh', 'ckyg'],
      hidden: false
    },
    children: [
      {
        path: "me",
        component: () => import("@/views/label-manage/me.vue"),
        name: "label-manageme",
        meta: {
          title: "我的订单",
          roles: ['yh'],
          hidden: false
        },
      },
      {
        path: "me_wl",
        component: () => import("@/views/label-manage/me_wl.vue"),
        name: "label-manageme_wl",
        meta: {
          title: "我的订单",
          roles: ['ckyg'],
          hidden: false
        },
      },
      {
        path: "index",
        component: () => import("@/views/label-manage/index.vue"),
        name: "label-manage",
        meta: {
          title: "领料单",
          roles: ["admin"],
          hidden: false
        },
      },
      {
        path: "ckd",
        component: () => import("@/views/label-manage/ckd.vue"),
        name: "label-manage2",
        meta: {
          title: "出库单",
          roles: ["admin", "ck", 'wl'],
          hidden: false
        }
      },
      // {
      //   path: "psd",
      //   component: () => import("@/views/label-manage/psd.vue"),
      //   name: "label-manage3",
      //   meta: {
      //     title: "配送单",
      //     roles: ["admin", 'wl'],
      //     hidden: false
      //   }
      // },
      {
        path: "zyd",
        component: () => import("@/views/label-manage/zyd.vue"),
        name: "label-manage1",
        meta: {
          title: "转运单",
          roles: ["admin", 'wl'],
          hidden: false
        }
      },
      // {
      //   path: "tuiku",
      //   component: () => import("@/views/label-manage/tuiku.vue"),
      //   name: "label-manage6",
      //   meta: {
      //     title: "退库单",
      //     roles: ["admin", "ck", 'yh'],
      //     hidden: false
      //   }
      // },
      {
        path: "rkd",
        component: () => import("@/views/label-manage/rkd.vue"),
        name: "label-manage4",
        meta: {
          title: "入库单",
          roles: ["admin", "ck"],
          hidden: false
        }
      },
      {
        path: "inventorying",
        component: () => import("@/views/label-manage/inventorying.vue"),
        name: "inventorying",
        meta: {
          title: "盘库",
          roles: [ "ckyg"],
        }
      },
      {
        path: "inventory",
        component: () => import("@/views/label-manage/inventory.vue"),
        name: "Inventory",
        meta: {
          title: "已盘库记录",
          roles: [ "ckyg"],
        }
      },
      {
        path: "over",
        component: () => import("@/views/label-manage/over.vue"),
        name: "label-manage5",
        meta: {
          title: "已完成",
          roles: ["admin", "ck", 'wl', 'yh', 'ckyg'],
          hidden: false
        }
      },
    ]
  },
  {
    path: "/card-manage",
    redirect: "/card-manage/todo",
    meta: {
      title: "审批管理",
      svgIcon: "sp",
      roles: ["admin", "ck", 'wl', 'yh'],
    },
    component: Layouts,
    children: [
      {
        path: "pressing",
        component: () => import("@/views/card-manage/pressing.vue"),
        name: "pressing",
        meta: {
          title: "加急审批",
        }
      },
      {
        path: "todo",
        component: () => import("@/views/card-manage/todo.vue"),
        name: "todo",
        meta: {
          title: "待办审批",
        }
      },
      {
        path: "passed",
        component: () => import("@/views/card-manage/passed.vue"),
        name: "passed",
        meta: {
          title: "审批通过",
        }
      },
      {
        path: "notPassed",
        component: () => import("@/views/card-manage/notPassed.vue"),
        name: "notPassed",
        meta: {
          title: "审批驳回",
        }
      },
    ]
  },
  {
    path: "/shipping",
    component: Layouts,
    redirect: "/shipping/index",
    meta: {
      // title: "客服",
      roles: ["admin", "ck"],
      svgIcon: "ck"
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/shipping/index.vue"),
        name: "shippingi",
        meta: {
          title: "发货单",
          svgIcon: "ck"
        }
      }
    ]
  },
  {
    path: "/system",
    meta: {
      title: "系统管理",
      svgIcon: "lock",
      roles: ["admin", "ck", 'wl'],
    },
    component: Layouts,
    children: [
      {
        path: "user",
        component: () => import("@/views/system/user/index.vue"),
        name: "systemUser",
        meta: {
          title: "用户管理",
        }
      },
      {
        path: "role",
        component: () => import("@/views/system/role/index.vue"),
        name: "systemRole",
        meta: {
          title: "角色管理",
        }
      },
      {
        path: "btns",
        component: () => import("@/views/system/but.vue"),
        name: "btns",
        meta: {
          title: "菜单管理",
        }
      },
    ]
  },
  {
    path: "/problem",
    component: Layouts,
    meta: {
      roles: ["admin", 'wl'],
    },
    redirect: "/problem/index",
    children: [
      {
        path: "index",
        component: () => import("@/views/problem/index.vue"),
        name: "problemindex",
        meta: {
          title: "客服",
          svgIcon: "ck"
        }
      }
    ]
  },
  {
    path: "/service",
    component: Layouts,
    redirect: "/service/index",
    meta: {
      roles: ["admin", 'wl', 'yh'],
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/problem/service.vue"),
        name: "servicei",
        meta: {
          title: "客服电话",
          svgIcon: "ck"
        }
      }
    ]
  },
  {
    path: "/organization",
    component: Layouts,
    redirect: "/organization/index",
    children: [
      {
        path: "index",
        component: () => import("@/views/problem/organization.vue"),
        name: "organization",
        meta: {
          title: "组织架构",
          svgIcon: "ck"
        }
      }
    ]
  },
]

const router = createRouter({
  history,
  routes: routeSettings.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
  try {
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    window.location.reload()
  }
}

export default router
